using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using  Xamarin.Auth;

namespace CPXamarin.Core
{
    public static class CPMyProfile
    {
        private static string clientId = "WRymObjweyg9fj78Z5FV3R-uHeoVt_Oh";
        private static string clientSecret = "NQyjvo7N7eN06Xu9nTHm4jRt0X7IZThNwPAKVnp9GBcOZKm89xIOhbeOIQrOXVJj";

        private static string baseUrl = "https://api.codeproject.com/";

        // this will hold the Access Token returned from the server.
        static string accessToken = "vxLUr5tm0oJX_9ASmeq7bnNJvgS68lAJlNsaK_WIpC-6tjnQMrWBga3POHtZ2LvTsxOdY7xxp9UGRr58kLld5Jg3eXxClzxPj49NQza4xJtBMxWqE2HDCcno4T_4lV6_k21aXRzYDO_8gZa7uZsSFSqrUDjvQZEkehD1WWUSXAaEIbYFzEhYvGSWeknvw4WnzSwuezZqLxGuEtGBQaPyqg9j774xTis3rw4t1LTQXNjuUkfyXwgDzEncUSQzUISj5HbTtB9O2Y-ZJKPb39bR_fWUmItR8zct6YlfDFJ5imW2yQnbJiNogzCRFJbZUd9LUFfPn7oQvAoCV9790PrhH0WJS2JDs54UtdZEeBMMxcm4qhICeQMTzRu6rYBxdGCJVVaSA0R6ZOHs8Mt6mU-J6tn_ZKLhkVX8JmvquB_dJpV0gzfjaUp0tBYvY7IPtillI8_14kaDCnDlUOflmi1iMUDiuESXigqHtRODuWgCLyNZyFIf";

        public static async Task<int> DoIt()
        {
            // Get the Access Token.
            Console.WriteLine(accessToken != null ? "Got Token" : "No Token found");
            if (accessToken != null)
            {
                var profile = await MyProfile();
               
                Console.WriteLine();
                Console.WriteLine("------ My Articles ------");
            }
            return 0;
        }

        private static async Task<int> MyProfile()
        {
        var baseUrl = "https://api.codeproject.com/";

        var accessToken = "vxLUr5tm0oJX_9ASmeq7bnNJvgS68lAJlNsaK_WIpC-6tjnQMrWBga3POHtZ2LvTsxOdY7xxp9UGRr58kLld5Jg3eXxClzxPj49NQza4xJtBMxWqE2HDCcno4T_4lV6_k21aXRzYDO_8gZa7uZsSFSqrUDjvQZEkehD1WWUSXAaEIbYFzEhYvGSWeknvw4WnzSwuezZqLxGuEtGBQaPyqg9j774xTis3rw4t1LTQXNjuUkfyXwgDzEncUSQzUISj5HbTtB9O2Y-ZJKPb39bR_fWUmItR8zct6YlfDFJ5imW2yQnbJiNogzCRFJbZUd9LUFfPn7oQvAoCV9790PrhH0WJS2JDs54UtdZEeBMMxcm4qhICeQMTzRu6rYBxdGCJVVaSA0R6ZOHs8Mt6mU-J6tn_ZKLhkVX8JmvquB_dJpV0gzfjaUp0tBYvY7IPtillI8_14kaDCnDlUOflmi1iMUDiuESXigqHtRODuWgCLyNZyFIf";
           
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response =null;
                // Add the Authorization header with the AccessToken.
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
                var numberOfAttempts = 0;
                // create the URL string.
                string url = string.Format("v1/My/Profile");
                do
                {
                    try
                    {
                         response =  await client.GetAsync(url);
                    }
                    catch (Exception es)
                    {
                        {
                            numberOfAttempts++;
                        }
                        
                    }
                } while (numberOfAttempts<4);

                

                // parse the response and return the data.
                string jsonString = await response.Content.ReadAsStringAsync();
                object responseData = JsonConvert.DeserializeObject(jsonString);

                return 1;
            }
        }
    }
}